package com.briup.ch08;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author briup-adam
 * @Date 2023/10/16 上午9:20
 * @Description
 **/

public class MapBasic {
    public static void main(String[] args) {
        Map map=new HashMap();
        //map存放元素 键唯一
        map.put("张锦","孙雅茹");
        //存放元素时  键相同 新值替换旧值
        System.out.println(map.put("张锦", "王旭"));
        System.out.println(map.size());//1
        //get  根据键去取值
        Object o = map.get("张锦");
        System.out.println(o);
        //获取不存在元素 返回null
        Object o1 = map.get("张矛");
        System.out.println(o1);
        System.out.println(map.containsKey("张锦"));//true
        System.out.println(map.containsValue("张锦"));//false
        Map map1=new HashMap();
        map1.put("海鹏","海螺");
        map1.put("小马","赤兔");
        map1.put("张美娜","张闭月");
        map1.put("白璐","白雪");
        map1.put("乔辉","乔治");
        map1.put("安大黔","云贵川");
        map1.put(null,"王亚文");
        map1.put("高斌",null);
        //将六个元素 添加到map
        map.putAll(map1);
        System.out.println(map.size());//7
        //获取所有的key
        Set set = map.keySet();
        for (Object o2 : set) {
            System.out.println(o2);
        }
        //获取所有的值
        Collection collection = map.values();
        for (Object o2 : collection) {
            System.out.println(o2);
        }
        //获取所有的键值对
        Set entrySet = map.entrySet();
        for (Object o2 : entrySet) {
            System.out.println(o2);
        }
        //map如何遍历
        //通过遍历keySet    获取键对应的值
        Set set1 = map.keySet();
        for (Object o2 : set1) {
            Object o3 = map.get(o2);
            System.out.println("key:"+o2+", value:"+o3);
        }
        //通过entrySet 遍历
        Set entrySet1 = map.entrySet();
        for (Object o2 : entrySet1) {
            Map.Entry entry= (Map.Entry) o2;
            System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
        }
    }
}
